/*
 * RemoteControl.java
 * Copyright (C) Jul 12, 2012 Bora SAYINER
 *
 * This file is part of Design Patterns.
 *
 * Design Patterns is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Design Patterns is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Design Patterns. If not, see http://www.gnu.org/licenses/.
 */
package net.samples.command;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * @author Bora SAYINER
 * @since Jul 12, 2012
 * @version 1.0.0
 */
public class RemoteControl extends TimerTask {

	private static RemoteControl instance;

	private final Timer timer = new Timer(true);
	private final ArrayList<ICommand> commands = new ArrayList<>();
	private final Ligth ligth = new Ligth();
	private final Garage garage = new Garage();
	private final DvdPlayer dvdPlayer = new DvdPlayer();

	private final LigthOnCommand ligthOnCommand = new LigthOnCommand(ligth);
	private final LigthOffCommand ligthOffCommand = new LigthOffCommand(ligth);
	private final GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(
			garage);
	private final GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(
			garage);
	private final DvdPlayerPlayCommand dvdPlayerPlayCommand = new DvdPlayerPlayCommand(
			dvdPlayer);
	private final DvdPlayerStopCommand dvdPlayerStopCommand = new DvdPlayerStopCommand(
			dvdPlayer);

	private RemoteControl() {
		addCommand();
	}

	public static synchronized RemoteControl getInstance() {
		if (instance == null) {
			instance = new RemoteControl();
		}
		return instance;
	}

	private void addCommand() {
		commands.add(ligthOnCommand);
		commands.add(ligthOffCommand);
		commands.add(garageDoorOpenCommand);
		commands.add(garageDoorCloseCommand);
		commands.add(dvdPlayerPlayCommand);
		commands.add(dvdPlayerStopCommand);
	}

	public void ligthsOn() {
		ligthOnCommand.execute();
	}

	public void ligthsOff() {
		ligthOffCommand.execute();
	}

	public void garageDoorOpen() {
		garageDoorOpenCommand.execute();
	}

	public void dvdPlay() {
		ligthsOff();
		dvdPlayerPlayCommand.execute();
	}

	public void dvdStop() {
		dvdPlayerStopCommand.execute();
	}

	public void startParty() {
		timer.schedule(this, 0, 1000);
	}

	public void stopPart() {
		System.out.println("Party is over");
		cancel();
	}

	public void execute(int commandNumber) {
		if (commandNumber > commands.size() || commandNumber < 0) {
			System.out.println("UnknowCommand");
		} else {
			commands.get(commandNumber).execute();
		}
	}

	@Override
	public void run() {
		for (ICommand command : commands) {
			command.execute();
		}
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
	}
}
